package com.info.zhiduoduo.weixin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.WxMessageService;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.system.SystemDictDto;
import com.info.zhiduoduo.repository.dto.value.ValueServeDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.maker.MakerStudio;
import com.info.zhiduoduo.repository.entity.value.ValueServe;
import com.info.zhiduoduo.repository.mapper.system.SystemDictMapper;
import com.info.zhiduoduo.repository.mapper.value.ValueServeMapper;
import com.info.zhiduoduo.weixin.formbean.value.QueryValueFormBean;
import com.info.zhiduoduo.weixin.formbean.value.SaveValueFromBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Service
public class ValueService extends BaseService {


    @Resource
    private ValueServeMapper valueServeMapper;

    @Resource
    private SystemDictMapper systemDictMapper;

    @Resource
    private WxMessageService wxMessageService;

    /**
     * 根据类型 获取 服务类型列表
     * @param baseSimpleFormBean
     * @return
     */
    public SmartResult queryServeTypeList(BaseSimpleFormBean<Integer> baseSimpleFormBean) {
        @Valid Integer type = baseSimpleFormBean.getReqdata();
        List<SystemDictDto> list = systemDictMapper.queryServeTypeList(type);
        return smartResult(list);
    }


    /**
     * 查询 列表
     * @param baseSimpleFormBean
     * @return
     */
    public SmartResult queryValueServeList(BaseSimpleFormBean<QueryValueFormBean> baseSimpleFormBean) {

        QueryValueFormBean formBean = baseSimpleFormBean.getReqdata();
        int id = baseSimpleFormBean.getUserid();
        if( id <= 0 ) return errorParamSmartResult();
        Integer state = formBean.getState();
        Integer type = formBean.getType();

        // 查询全部
        if( state == 4 ){
            state = null;
        }

        Page<ValueServeDto> page = PageHelper.startPage(baseSimpleFormBean.getPage(), baseSimpleFormBean.getRows());
        valueServeMapper.queryValueServeList(id, state, type);
        return smartResultPage(page);

    }

    /**
     * 添加或修改
     * @param baseSimpleFormBean
     * @return
     */
    public SmartResult addOrEditValueServe(BaseSimpleFormBean<SaveValueFromBean> baseSimpleFormBean) {

        SaveValueFromBean formBean = baseSimpleFormBean.getReqdata();

        if( formBean.getId() > 0 ){
            // 表示修改
            ValueServe valueServe = new ValueServe().selectById( formBean.getId() );
            if( valueServe == null || valueServe.getState() == 1 ) return optFailSmartResult();

            valueServe.setType( formBean.getType() );
            valueServe.setRemark( formBean.getRemark() );
            valueServe.setUpdatetime( new Date());

            if( !valueServe.updateById() ){
                return optFailSmartResult();
            }

        } else {

            String name = "";
            if (formBean.getCustomerType() == 1 || formBean.getCustomerType() == 5 || formBean.getCustomerType() == 6) {
                MakerStudio makerStudio = new MakerStudio().selectById(formBean.getCustomerId());
                if (makerStudio == null) return errorParamSmartResult();
                name = makerStudio.getUsername();
            } else if (formBean.getCustomerType() == 2) {
                Company company = new Company().selectById(formBean.getCustomerId());
                if (company == null) return errorParamSmartResult();
                name = company.getCompname();
            } else {
                return errorParamSmartResult();
            }

            // 添加
            ValueServe valueServe = new ValueServe();
            valueServe.setCustomerId( formBean.getCustomerId() ).setCustomerType( formBean.getCustomerType() )
                    .setName( name ).setType( formBean.getType() ).setRemark( formBean.getRemark() )
                    .setState( 0 );
            if( !valueServe.insert() ){
                return optFailSmartResult();
            }
        }
        if (formBean.getCustomerType() == 1 || formBean.getCustomerType() == 5 || formBean.getCustomerType() == 6) {
            wxMessageService.makerAppreciationServiceApplyNotice(formBean.getCustomerId(), formBean.getType());
        } else {
            wxMessageService.companyAppreciationServiceApplyNotice(formBean.getCustomerId(), formBean.getType());
        }
        return smartResult();
    }
}
